package com.example.projectjava.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

//针对appUser表做SQL操作的类
@Repository   //此注解的作用：与控制器中的@Controller注解的功能相同，意为将该类纳入Spring容器管理
public class CategoryDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public  int insertCategory(String category_id,String category_name,String product_count){
        String sql = "insert into product_category(category_id,category_name,product_count) values(?,?,?)";
        return    jdbcTemplate.update(sql,category_id,category_name,product_count);
    }

    public  int deleteCategory(String category_id){
        String sql  = "DELETE from product_category where category_id=?";
        return  jdbcTemplate.update(sql,category_id);
    }

    public List<Map<String,Object>> queryAllCategory(){
        String sql = "select * from product_category";
        return  jdbcTemplate.queryForList(sql);
    }

    public int updateCategory(String category_id, String category_name, String product_count){
        String sql = "update product_category set category_name=?,product_count=? where category_id=?";
        return  jdbcTemplate.update(sql,category_name,product_count,category_id);
    }

    public List<Map<String,Object>> queryProductByCategory(String category_id){
        String sql = "select * from product where category_id=?";
        return  jdbcTemplate.queryForList(sql,category_id);
    }
}
